<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
date_default_timezone_set ( 'prc' );
class Shoppingcart extends CI_Controller {
	public function __construct() {
		parent::__construct ();
	}
	/*
	 * 添加商品到购物车
	 */
	public function addtocart() {
		$userid = $this->session->userdata ( 'id' );
		if(empty($userid)){
			return_json ( FALSE, '', '请重新登录后再操作！' );
		}
		$this->load->model ( 'user_model' );
		$user = $this->user_model->load($userid);
		$this->load->model ( 'config_model' );
		$config = $this->config_model->find_by_key ("state");
		if($config['value']==1){
			return_json ( FAlSE, '','当前时间无法下单！' );
		}
		if($user['order_state']){
			return_json ( FALSE, '','您本周已订餐！');
		}
		$productid = $this->input->post ( 'productid' );
		$amount = $this->input->post ( 'amount' );
		$date = $this->input->post ( 'date' );
		$type = $this->input->post ( 'type' );
		$this->load->model ( 'shoppingcart_model' );
		$this->load->model ( 'product_model' );
		if($amount<1){
			return_json ( FALSE, '','数量必须大于0！');
		}
		$product = $this->product_model->find_by_school_date_product_type($user['school'],$date,$productid,$type);
		if(!empty($product)&&$amount>$product['stock']){
			return_json ( FALSE, '','剩余数量不足！');
		}
		$shoppingcart = $this->shoppingcart_model->find_by_user_date_type ( $userid,$date,$type);//用户某天某餐是否已订
		if (! empty ( $shoppingcart ['id'] )) {
			$newshoppingcart = array (
				'productid' => $productid,
				'amount' => $amount
			);
			if ($this->shoppingcart_model->update ( $shoppingcart ['id'], $newshoppingcart )) {
				$total=$this->get_amount($userid);
				return_json ( TRUE, $total, '更新购物车成功！' );
			} else {
				return_json ( FALSE, '', '商品没有变化！' );
			}
		} else {
			$newshoppingcart = array (
				'productid' => $productid,
				'amount' => $amount,
				'userid' => $userid,
				'date' => $date,
				'type' => $type,
			);
			$shoppingcartid=$this->shoppingcart_model->create ( $newshoppingcart );
			if (!empty($shoppingcartid)) {
				$total=$this->get_amount($userid);
				return_json ( TRUE, $total, '添加到购物车成功！' );
			} else {
				return_json ( FALSE, '', '添加到购物车失败！' );
			}
		}
	}
	/*
	 * 跳转到购物车页面
	 */
	public function index() {
		$userid = $this->session->userdata ( 'id' );
		if (empty($userid)) {
			redirect ( 'passport/index' );
		}
		$this->load->model ( 'user_model' );
		$user = $this->user_model->load($userid);
		$this->load->model ( 'shoppingcart_model' );
		$shoppingcarts= $this->shoppingcart_model->find_by_user ( $userid );
		$data ['shoppingcart'] =$shoppingcarts;
		$data['date']= $this->common->getOrderDate($user['school']);
		$this->load->view ( 'shoppingcart/index', $data );
	}
	/*
	 * 移除购物车中的商品
	 */
	public function delete() {
		$userid = $this->session->userdata ( 'id' );
		if(empty($userid)){
			return_json ( FALSE, '', '请重新登录后再操作！' );
		}
		$shoppingcartid = $this->input->post('id');
		$this->load->model ( 'shoppingcart_model' );
		if ($this->shoppingcart_model->delete ($shoppingcartid )) {
			return_json ( TRUE, '', '移除成功！' );
		}else{
			return_json ( FALSE, '', '移除失败！' );
		}
	}
	/*
	 * 清空购物车
	 */
	public function clear() {
		$userid = $this->session->userdata ( 'id' );
		if(empty($userid)){
			return_json ( FALSE, '', '请重新登录后再操作！' );
		}
		$this->load->model ( 'shoppingcart_model' );
		if ($this->shoppingcart_model->delete_by_user ( $userid )) {
			return_json ( TRUE, '', '清空成功！' );
		}else{
			return_json ( FALSE, '', '清空失败！' );
		}
	}
	/*
	 * 返回用户购物车内商品的数量
	 */
	private function get_amount($userid) {
		$this->load->model ( 'shoppingcart_model' );
		$carts = $this->shoppingcart_model->find_by_user ( $userid );
		return count($carts);
	}
}